<?php

/**
 * Description of e_mail
 *
 *  Yefng
 */
namespace c\user;

class e_mail extends \control
{
	var $class = array('PU'=>'\c\auth\permission');
	
    // 邮箱修改
    function index_home()
    {
        $this->PU->login();
        if (isset($_POST['email']))
            $this->e1();
        elseif (isset($_GET['q']))
         {
            $q = \encrypt::q_de($_GET['q']);
			parse_str($q, $data);
         }
        $this->loadTemplate('user/edit-mail', array('d'=>$data));
    }
	// 发送绑定链接
    protected function e1()
    {
        $d = $_POST;
		$uid = \YF_f::$login_user_id;
		$b = $this->db->getOne($this->table, '*',  'email="'. $this->db->escape($d['email'])  .'"');
		if ($b)
			$this->clue(\lang::g('email already exist other user'), '?u=cuser\my');
		
        //解绑
        if (\YF_f::$arr['login_user']['email'])
         {
			$mark = 'my-update-email-'. $uid;
			$Ctem = \YF_f::initC('\c\temp\db');
            $a = $Ctem->find_del($mark);
            if (!$a or  UNIX_TIME - $a['create_time']>3600*48)
                $this->clue(\lang::g('Verification code has expired.'));
            if ($d['ver']!=$a['val'])
                $this->clue(\lang::g('Verification code error.'));
			$this->db->save($this->table, array('email'=>''), 'id='. $uid);
			\log::write('email_edit', \YF_f::$arr['login_user']['email']);
         }
   
        if (strpos($d['email'], '@')===false)
            $this->clue(\lang::g('user name wrongful'));
        $url = 'e='.$d['email'] . '&i='. $uid;
		$url = ROOT_URL .'?u=cuser\e_mail-fe2&q='. rawurlencode(\encrypt::authcode($url, 24*3*3600));
        $b = \YF_f::initC('mail')->send($d['email'], \lang::g('mail_t_active_email'), sprintf(\lang::g('mail_c_active_email %s'), $url, $url));
        if ($b)
            die(\lang::g($b));
        $this->clue(\lang::g('go email active'), '?u=cuser\my');
    }
	//绑定激活
	function index_e2()
	{
		$q = \encrypt::authcode($_GET['q']);
		$this->isExist($q);
		parse_str($q, $d);
		
		$b = $this->db->getOne($this->table, '*',  'email="'. $this->db->escape($d['email'])  .'"');
		if ($b)
			$this->clue(\lang::g('email already exist other user'), '?u=cuser\my');
		$b = $this->db->save($this->table, array('email'=>$d['email']), 'id='. intval($d['id']).' AND email=""');
        \log::write('email_save', \YF_f::$arr['login_user']['email']);
        $this->clue(\lang::g('OK'), '?u=cuser\my');
	}
	
	//认证吗
    function index_send()
    {
        $this->PU->login();
        $email = \YF_f::$arr['login_user']['email'];
        if ($email=='')
            die(\lang::g('not set email'));
        
        $tempC = \YF_f::initC('\c\temp\db');
        $mark = 'my-update-email-'. \YF_f::$login_user_id;
        $a = $tempC->findRow($mark);
        if ($a && UNIX_TIME - $a['create_time']<60)
            die("0");
        
        //mail
        $No = randomkeys(6);
        $url = ROOT_URL .'?u=cuser\e_mail&q='. \encrypt::q_ed("v=$No");
        $b = \YF_f::initC('mail')->send($email, \lang::g('mail_t_update_email'), sprintf(\lang::g('mail_c_update_email %s'), $No, $url, $url));
        if ($b)
            die(\lang::g($b));
        
        $tempC->save($mark, $No);
        die("0");
    }
}
